package com.zjw.zy.heart.domain;

import java.io.Serial;
import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.zjw.zy.heart.utils.JsonUtils;
import com.zjw.zy.heart.utils.LogUtils;
import jakarta.annotation.Nullable;
import lombok.Data;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @TableName t_app_user
 */
@Data
public class User implements Serializable {


    public static final String BIND_INFO_KEY = "BIND_INFO_KEY";

    /**
     *
     */
    @JsonProperty("username")
    private String username;

    /**
     *
     */
    @JsonProperty("love_username")
    private String loveusername;

    /**
     *
     */
    @JsonProperty("info")
    private String info;

    /**
     *
     */
    @JsonProperty("address")
    private String address;

    /**
     *
     */
    @JsonProperty("name")
    private String name;

    @Serial
    private static final long serialVersionUID = 1L;

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        User other = (User) that;
        return (this.getUsername() == null ? other.getUsername() == null : this.getUsername().equals(other.getUsername())) && (this.getLoveusername() == null ? other.getLoveusername() == null : this.getLoveusername().equals(other.getLoveusername())) && (this.getInfo() == null ? other.getInfo() == null : this.getInfo().equals(other.getInfo())) && (this.getAddress() == null ? other.getAddress() == null : this.getAddress().equals(other.getAddress())) && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getUsername() == null) ? 0 : getUsername().hashCode());
        result = prime * result + ((getLoveusername() == null) ? 0 : getLoveusername().hashCode());
        result = prime * result + ((getInfo() == null) ? 0 : getInfo().hashCode());
        result = prime * result + ((getAddress() == null) ? 0 : getAddress().hashCode());
        result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
        return result;
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() +
                " [" +
                "Hash = " + hashCode() +
                ", username=" + username +
                ", loveusername=" + loveusername +
                ", info=" + info +
                ", address=" + address +
                ", name=" + name +
                ", serialVersionUID=" + serialVersionUID +
                "]";
    }

    public void putInfo(String key, Object payload) {
        JSONObject object = new JSONObject(info == null ? "{}" : info);
        object.put(key, JsonUtils.toJson(payload));
        info = object.toString();
    }

    @Nullable
    public <T> T getInfo(String key, Class<T> clazz) {
        try {
            JSONObject object = new JSONObject(info == null ? "{}" : info);
            String opt = object.optString(key);
            return JsonUtils.fromJson(opt, clazz);
        } catch (Exception e) {
            LogUtils.e("user", e.getMessage());
        }
        return null;
    }
}